local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local TweenHelper = _G.CommonOHOH.load.TweenHelper();
local BtnHelper = ClientOHOH.load.BtnHelper();
local CarTool=_G.CommonOHOH.load.CarTool();
local LanguageHelper=_G.LanguageHelper 
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();

local Tool=require(script.Parent.tool)

local instStr={

    CF="ctrl.showBox.CF",
    ViewportFrame="ctrl.showBox.ViewportFrame",

    btn="ctrl.showBox.btnSkin",--装备当前选择的部件，锁定
    btnNew="ctrl.showBox.btn",--装备当前最新的部件，解锁

    bit="ctrl.showBox.carData.bit",
    decoration="ctrl.showBox.carData.decoration",
    propulsion="ctrl.showBox.carData.propulsion",
    seat="ctrl.showBox.carData.seat",
    tire="ctrl.showBox.carData.tire",
}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
    M.super.ctor(self);
    self.ctrl=ctrl
    self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)

    self.displayEquip =true 
    self.skinData = nil

    CallBackManager:AddRbxConnect(self,self.ui.btn.Activated, function()
		self.ctrl.SkinMakeUpEvent:Action()
 	 end);
    CallBackManager:AddRbxConnect(self,self.ui.btnNew.Activated, function()
		self.ctrl.NewMakeUpEvent:Action()
 	 end);
end;

local tw1 = TweenInfo.new(12,  Enum.EasingStyle.Linear,Enum.EasingDirection.Out,-1,false,0);

function M.RoatateModel(self,v)
    TweenHelper:PlayTween(self.AniamtionValue, tw1, {
        Value =v
    });
end

function M.DisplayModel(self,bodyId,flagId,ringId,tailId,weaponId)
    print("******* 展示模型",bodyId,flagId,ringId,tailId,weaponId)
    if  not bodyId then
        return
    end 
    self.car= _G.CommonOHOH.load.Car().new(nil,bodyId,flagId,ringId,tailId,weaponId)
    
    self.car:SetParent(self.ui.ViewportFrame)
    self.car:SetCFrame(self.ui.CF)

    self.car:AnchoredSwitch(true)

    self.AniamtionValue= Instance.new("IntValue",self.car:GetModel())
    self.AniamtionValue.Value=0

        self.AniamtionValue.Changed:Connect(function(value)
        -- local cf=self.Model:GetPrimaryPartCFrame()*CFrame.Angles(0,math.rad(2),0)
        local cf=self.ui.CF*CFrame.Angles(0,math.rad(value),0)
        self.car:SetCFrame(cf)
    end)

    self:RoatateModel(360)

 --   self.car:SetDrillingSpeed(300)

end;

function M.DestroyModel(self)
   if  self.car then
    self.car.Model:Destroy()   
    self.car = nil
   end
end;

function M.DisplayChange(self,cell)
    -- print("******* 展示改变",cell:GetCarId())
    local displayData = {}
    -- for i, v in pairs(self.skinData) do
    -- displayData[i] = v
    -- end
    -- print("******* 展示改变 1 ",displayData)
    if not self.displayData then
       
        self.displayData={}
        for i, v in pairs(self.skinData) do
            self.displayData[i] = v
        end
    end
    local carID = cell:GetCarId()
    local IDReplace =string.sub(carID, 1, 1)
    -- print("******* 展示改变 2 ",displayData)
    for nowId, status in pairs(self.displayData) do
     ---判断装备的carId是否是已有的
     print("    替换中1 nowId, status：", nowId, status)
        if carID == nowId then
            break
        end
        local IDmatch =string.sub(nowId, 1, 1)
        print("    替换中2 IDmatch：", IDmatch)
        ---判断carId第一位的值是否相等，相等则为同类型，将其替换
        if IDmatch == IDReplace then
            -- displayData[nowId] = nil
            displayData[carID] = status
            -- print("    替换结束   IDmatch：", nowId,carID, displayData)
            -- break 
        else
            displayData[nowId] = status
        end
    end
    self.displayData ={}
    for i, v in pairs(displayData) do
        self.displayData[i] = v
    end


    print("******* 展示改变 4 ",self.displayData)
    --self.displayData = displayData
    self:DestroyModel()
    self:DisplayModel(Tool.GetAllCarIdBySkinData(self.displayData))

end;

function M.DisplayDefault(self)
    print("****展示默认")
    self:DestroyModel()
    self:DisplayModel(Tool.GetAllCarIdBySkinData(self.skinData))
end;
function M.StringFormat(self,carId)
    local carCfg = CarTool.GetPartAllInfo(carId).cfg
    
    local string1,string2 
    for index, value in pairs(carCfg.value) do
    
        local emojiTxt = LanguageHelper:GetText(5551000+carCfg.property[index]) 
        local numTxt =nil
        if carCfg.valueType == 1 then
            numTxt = BigNumberHelper:ToString(carCfg.value[1])
        else
            numTxt = BigNumberHelper:ToString(carCfg.value[1]*100).."%" 
        end

        local textId=self.ui.tire:GetAttribute("textID_Arg")

        if index ==1 then
            string1 =  "+"..numTxt.." "..emojiTxt
        else
            string2 =   ";+"..numTxt.." "..emojiTxt
        end
		
    
    end
    if string2 then
        return string1..string2
    else
        return string1
    end
end
function M.DataChange(self)
 
    local ringId = self.equipData[2]

    if ringId  then
    local ringTxt = self:StringFormat(ringId)
    self.ui.tire.Text = ringTxt
    end

    local bitId = self.equipData[4]
    if  bitId  then
    local bitTxt = self:StringFormat(bitId)
    self.ui.bit.Text = bitTxt
    end
    local decorationId = self.equipData[5]
    if  decorationId then
    local decorationTxt = self:StringFormat(decorationId)
    self.ui.decoration.Text = decorationTxt
    end

    local propulsionId = self.equipData[3]
    if propulsionId  then
    local propulsionTxt = self:StringFormat(propulsionId)
    self.ui.propulsion.Text = propulsionTxt
    end

    local seatId = self.equipData[1]
    if seatId  then
    local seatTxt = self:StringFormat(seatId)
    self.ui.seat.Text = seatTxt
   end
end;



function M.dtor(self)
	
end;


function M.UpdateSkin(self,data)
    print("******** 收到更新 皮肤展示********")
    if self.skinData  then
        
        local isSame =Tool.IsSkinDataSame(self.skinData,data)
        if not isSame  then
            self:DestroyModel()
            self:DisplayModel(Tool.GetAllCarIdBySkinData(data))
        end 
    else
        if not self.displayData then
            self.displayData={}
            for i, v in pairs(data) do
                self.displayData[i] = v
            end
        end
        self:DestroyModel()
        self:DisplayModel(Tool.GetAllCarIdBySkinData(data))
    end
    self.skinData  = {}
    for i, v in pairs(data) do
        self.skinData[i] = v
    end
    -----------------------------------------------------------------
    -- if self.skinData  then
        
    --     local isSame =Tool.IsSkinDataSame(self.skinData,data)
    --     if not isSame  then
    --         self:DestroyModel()
    --         self:DisplayModel(Tool.GetAllCarIdBySkinData(data))
    --     end 
    -- else
    --     self:DestroyModel()
    --     self:DisplayModel(Tool.GetAllCarIdBySkinData(data))
    -- end
    -- self.skinData  = {}
    -- for i, v in ipairs(data) do
    --     self.skinData[i] = v
    -- end
end
function M.UpdateEquip(self,data)
    self.equipData = data
    self:DataChange()
end



return M